package com.dengdai.applibrary.https;

import android.content.Context;
import android.util.Log;

import com.dengdai.applibrary.base.BaseApplication;
import com.dengdai.applibrary.utils.LogUtils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @说明 Http请求方法
 * @作者 LY
 * @文件 HttpHelper.java
 * @时间 2015年6月11日 下午9:36:42
 * @版权 Copyright(c) 2014 LY-版权所有
 */
public class HttpHelper {
    private static HttpHelper mInstance;// 自身实例
    private String httpUrl = "";
    private static Context mContext;
    private static Map<String, String> topInfo;

    /**
     * 单例同步模式
     *
     * @param context
     * @return
     */
    public synchronized static HttpHelper getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new HttpHelper();
        }
        mContext = context;
        return mInstance;
    }

    /**
     * 设置网址
     *
     * @param url
     */
    public void setHtmlUrl(String url) {
        this.httpUrl = url;
    }

    /**
     * 发送POST请求
     *
     * @param action 方法名
     * @param params 参数
     * @return 服务器返回值
     */
    public String sendPost(String action, Map<?, ?> params) {
        return send(HttpMethods.POST_METHODS, action, params);
    }

    /**
     * 发送GET请求
     *
     * @param action 方法名
     * @param params 参数
     * @return 服务器返回值
     */
    public String sendGet(String action, Map<?, ?> params) {
        return send(HttpMethods.GET_METHODS, action, params);
    }

    /**
     * 发送JSON请求
     *
     * @param action 方法名
     * @param json   参数
     * @return 服务器返回值
     */
    public String sendJson(String action, String json) {
        if (null != json) {
            Log.v("hanjiahu json", json);
        }
        return send(HttpMethods.JSON_METHODS, action, json);
    }

    /**
     * 发送DELETE请求
     *
     * @param action 方法名
     * @param params 参数
     * @return 服务器返回值
     */
    public String sendDelete(String action, Map<?, ?> params) {
        return send(HttpMethods.DELETE_METHODS, action, params);
    }

    /**
     * 发送PUT请求
     *
     * @param action 方法名
     * @param params 参数
     * @return 服务器返回值
     */
    public String sendPut(String action, Map<?, ?> params) {
        return send(HttpMethods.PUT_METHODS, action, params);
    }

    /**
     * 文件上传
     *
     * @param action   方法名
     * @param filePath 文件路径
     * @return
     * @throws Exception
     */
    public String uploadFile(String action, String filePath) {
        HttpRequester request = new HttpRequester();
        request.setDefaultContentEncoding("utf-8");
        HttpRespons hr = null;
        try {
            hr = request.uploadFile(httpUrl + action, filePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return hr.getContent();
    }

    /**
     * 发送请求
     *
     * @param method 请求方式
     * @param action 方法名
     * @param params 参数
     * @return 服务器返回值
     * @throws Exception 异常信息
     */
    @SuppressWarnings("unchecked")
    private String send(String method, String action, Object params) {
        HttpRequester request = new HttpRequester();
        request.setDefaultContentEncoding("utf-8");
        String urlString = httpUrl + action;
        HttpRespons hr = null;
        try {
            if (HttpMethods.GET_METHODS.equals(method)) {
                Map<String, String> p = (HashMap<String, String>) params;
                itMap(p);
                hr = request.sendGet(urlString, p, null);
            } else if (HttpMethods.POST_METHODS.equals(method)) {
                Map<String, String> p = (HashMap<String, String>) params;
                itMap(p);
                hr = request.sendPost(urlString, p, null);
            } else if (HttpMethods.JSON_METHODS.equals(method)) {
                String p = (String) params;
                hr = request.sendJson(urlString, p, null);
            } else if (HttpMethods.DELETE_METHODS.equals(method)) {
                Map<String, String> p = (HashMap<String, String>) params;
                itMap(p);
                hr = request.sendDelete(urlString, p, null);
            } else if (HttpMethods.PUT_METHODS.equals(method)) {
                Map<String, String> p = (HashMap<String, String>) params;
                itMap(p);
                hr = request.sendPut(urlString, p, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (hr == null) {
            return null;
        } else {
            return hr.getContent() == null ? null : hr.getContent().replaceAll("\r\n", "");
        }
    }

    /**
     * 请求网址
     *
     * @param httpUrl http地址
     * @return 请求数据
     */
    public HttpRespons sendUrl(String httpUrl) {
        HttpRequester request = new HttpRequester();
        request.setDefaultContentEncoding("utf-8");
        HttpRespons hr = null;
        try {
            hr = request.sendGet(httpUrl, null, null);
        } catch (Exception e) {
        }
        return hr;
    }

    private static void itMap(Map<String, String> map) {
        if (map != null) {
            if (topInfo != null) {
                Set<String> keys = topInfo.keySet();
                for (Iterator<String> it = keys.iterator(); it.hasNext(); ) {
                    String key = it.next();
                    map.put(key, topInfo.get(key));
                }
            }
        }
    }

    /**
     * 添加请求头
     *
     * @param propertys
     */
    public void addTopInfo(Map<String, String> propertys) {
        this.topInfo = propertys;
    }
}